package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysMsg;
import com.ruoyi.system.service.ISysMsgService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 消息Controller
 * 
 * @author ruoyi
 * @date 2025-05-10
 */
@Controller
@RequestMapping("/system/msg")
public class SysMsgController extends BaseController
{
    private String prefix = "system/msg";

    @Autowired
    private ISysMsgService sysMsgService;

    //@RequiresPermissions("system:msg:view")
    @GetMapping("/{msgType}")
    public String msg(@PathVariable String msgType, ModelMap mmap)
    {

        mmap.put("msgType", msgType);
        return prefix + "/msg";
    }

    /**
     * 查询消息列表
     */
    //@RequiresPermissions("system:msg:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysMsg sysMsg)
    {
        if(sysMsg != null && "all".equals(sysMsg.getType())){
            sysMsg.setType(null);
        }
        startPage();
        sysMsg.setReceiverId(ShiroUtils.getSysUser().getUserId());
        List<SysMsg> list = sysMsgService.selectSysMsgList(sysMsg);
        return getDataTable(list);
    }

    /**
     * 导出消息列表
     */
    @RequiresPermissions("system:msg:export")
    @Log(title = "消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysMsg sysMsg)
    {
        List<SysMsg> list = sysMsgService.selectSysMsgList(sysMsg);
        ExcelUtil<SysMsg> util = new ExcelUtil<SysMsg>(SysMsg.class);
        return util.exportExcel(list, "消息数据");
    }

    /**
     * 新增消息
     */
    @RequiresPermissions("system:msg:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存消息
     */
    @RequiresPermissions("system:msg:add")
    @Log(title = "消息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysMsg sysMsg)
    {
        return toAjax(sysMsgService.insertSysMsg(sysMsg));
    }

    /**
     * 修改消息
     */
    @RequiresPermissions("system:msg:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysMsg sysMsg = sysMsgService.selectSysMsgById(id);
        mmap.put("sysMsg", sysMsg);
        return prefix + "/edit";
    }

    /**
     * 修改保存消息
     */
    @RequiresPermissions("system:msg:edit")
    @Log(title = "消息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysMsg sysMsg)
    {
        return toAjax(sysMsgService.updateSysMsg(sysMsg));
    }

    /**
     * 删除消息
     */
    //@RequiresPermissions("system:msg:remove")
    @Log(title = "消息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysMsgService.deleteSysMsgByIds(ids));
    }

    /**
     * 删除消息
     */
    //@RequiresPermissions("system:msg:remove")
    //@Log(title = "消息", businessType = BusinessType.DELETE)
    @PostMapping( "/readFlag")
    @ResponseBody
    public AjaxResult readFlag(String ids)
    {
        sysMsgService.readFlagByIds(ids);
        return toAjax(1);
    }

    @PostMapping( "/readFlagAll")
    @ResponseBody
    public AjaxResult readFlagAll()
    {
        sysMsgService.readFlagByReceiverId(ShiroUtils.getSysUser().getUserId());
        return toAjax(1);
    }
}
